//app.js
App({
  d:{
    hostUrl : 'https://www.gengyong.com.cn',
    userId : '',
  },

  //预加载
  onLaunch: function () {
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs',logs)
    this.getUserInfo()
  },

  //获取用户信息
  getUserInfo:function(cb){
    var that = this;
    if(this.globalData.userInfo){
      typeof cb == "function" && cb(this.globalData.userInfo)
    }else{
      wx.login({
        success:function(res){
          var code = res.code;
          wx.getUserInfo({
            success:function(res){
              that.globalData.userInfo = res.userInfo;
              typeof cb == "function" && cb(this.globalData.userInfo)
              that.getUserSessionKey(code);
            }
          });
        }
      });
    }
  },

  //获取session_key，openID
  getUserSessionKey:function(code){
    var that = this;

    wx.request({
      url: that.d.hostUrl+'/api/user/getSessionKey',
      method: 'get',
      data:{
        code:code
      },
      header: {
        'Content-Type':  'application/x-www-form-urlencoded'
      },

      success:function(res){
        var data = res.data;

        if(data.status == 0){
          wx.showToast({
            title: data.info,
            duration: 2000
          });
          return false;
        }

        that.globalData.userInfo['sessionId'] = data.session_key;
        that.globalData.userInfo['openid'] = data.openid;
        //授权登录获取用户信息
        that.onLoginUser();
      },

      fail:function(e){
        wx.showToast({
          title:'网络异常！',
          duration:2000
        });
      }
    })
  },

  //授权登录获取比对用户信息
  onLoginUser:function(){
    var that = this;
    var user = that.globalData.userInfo;
    wx.request({
      url : that.d.hostUrl+'/api/user/authLogin',
      method : 'get',
      data : {
        sessionId : user.sessionId,
        username : user.nickName,
        gender : user.gender,
        headurl : user.avatarUrl,
        openid : user.openid,
        country : user.country,
        province : user.province,
        city : user.city
      },

      header: {
        'Content-Type':  'application/x-www-form-urlencoded'
      },

      success:function(res){
        var data = res.data.info;
        var status = res.data.status;

        if(status != 1){
          wx.showToast({
            title:data,
            duration:2000
          });
          return false;
        }

        that.globalData.userInfo['uid'] = data.id;
        that.globalData.userInfo['nickName'] = data.nickname;
        that.globalData.userInfo['avatarUrl'] = data.avatarUrl;
        that.globalData.userInfo['integral'] = data.integral;
        var userId = data.id;
        if(!userId){
          wx.showToast({
            title:'登陆失败！',
            duration:2000
          });
          return false;
        }
        that.d.userId = userId;
      },

      fail:function(res){
        wx.showToast({
          title : '网络异常！',
          duration : 2000
        });
      }
    });
  },


  //将用户信息设置为全局变量
  globalData:{
    userInfo:null
  },

})